#!/usr/bin/env python
# -*- coding: utf-8 -*-


'''
key ::= row-key column-family column-qualifier flag timestamp
flag ::= DELETE_ROW | DELETE_COLUMN_FAMILY | DELETE_CELL | INSERT

DELETE_ROW           ::= 0x00
DELETE_COLUMN_FAMILY ::= 0x01
DELETE_CELL          ::= 0x02
INSERT               ::= 0xFF
'''

import sys
import struct
import time

def test1() :
    for rowkey in ('foo', 'bar','baz', 'buzz') :
        family = 17
        qualifier = 'tag:test'
        flag = 0x02
        timestamp = int(time.time())
        key = struct.pack('>s',     rowkey)
        key = struct.pack('>sB',    rowkey, family)
        key = struct.pack('>sBs',   rowkey, family, qualifier)
        key = struct.pack('>sBsB',  rowkey, family, qualifier, flag)
        key = struct.pack('>sBsBI', rowkey, family, qualifier, flag, timestamp)

        (rowkey_back, family_back, qualifier_back, flag_back, timestamp_back) = struct.unpack('>sBsBI', key)
        if rowkey != rowkey_back :
            print 'BAD RowKey:','rowkey =',rowkey,' BACK =',rowkey_back
        if family != family_back :
            print 'BAD Family:','family =',family,' BACK =',family_back
        if qualifier != qualifier_back :
            print 'BAD Qualifier:','qualifier =',qualifier,' BACK =',qualifier_back
        if flag != flag_back :
            print 'BAD Flag:','flag =',flag,' BACK =',flag_back
        if timestamp != timestamp_back :
            print 'BAD Timestamp:','timestamp =',timestamp,' BACK =',timestamp_back

    # test1()

#######################################################################

if  __name__=='__main__' :
    try :
        test1()
    except struct.error :
        print sys.exc_info()
        raise
    except :
        raise
